import java.util.Scanner;

public class test3 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] nums = new int[n];
        for (int i = 0; i < n; i++) {
           nums[i] = in.nextInt();
        }
        System.out.println(maxProfit1(nums));
    }
    public static int maxProfit1(int[] prices) {
        // 动态规划 -- > 只能买一次
        int n = prices.length;
        int[] dp1 = new int[n];
        int[] dp2 = new int[n];
        dp1[0] = -prices[0];
        for (int i = 1; i < n; i++) {
            dp1[i] = Math.max(dp1[i-1],dp2[i-1]-prices[i]);// 买入点
            dp2[i] = Math.max(dp2[i-1],dp1[i-1]+prices[i]);// 卖出点
        }
        return dp2[n-1];
    }

    public int maxProfit(int[] prices) {
        // 贪心 用一个变量标记前缀最小值
        int flag = Integer.MAX_VALUE;
        int result = 0;
        for(int x:prices){
            if (x<flag)
                flag = x;
            if(x-flag>result)
                result = x-flag;
        }
        return result;
    }
}
